<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置
            <small>角色管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>角色管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="角色名称/关键字" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <!--<el-table-column prop="id" label="id" align="center"></el-table-column>-->
                <el-table-column prop="name" label="角色名称" align="center"></el-table-column>
                <el-table-column prop="keyword" label="关键字" align="center"></el-table-column>
                <el-table-column prop="description" label="说明" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                        <el-button size="mini" type="info" @click="roleleEdit(scope.row)">修改权限菜单</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增角色层 -->
            <div class="add-form">
                <el-dialog title="新增角色" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px" ref="dataAddForm" :model="formData"
                                         :rules="rules">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="角色名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="关键字" prop="keyword">
                                                <el-input v-model="formData.keyword"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="说明">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 编辑角色层 -->
            <div class="add-form">
                <el-dialog title="编辑角色" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px" ref="dataSetForm" :model="formData"
                                         :rules="rules">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="关键字" prop="keyword">
                                                <el-input v-model="formData.keyword"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="说明">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>


            <!--编辑分配权限菜单层-->
            <div class="add-form">
                <el-dialog title="分配菜单/权限" :visible.sync="AccessTheMenu">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="分配菜单" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="名称">
                                                <el-input v-model="formData.name" readonly="readonly"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="关键字">
                                                <el-input v-model="formData.keyword" readonly="readonly"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                                <table class="datatable">
                                    <thead>
                                    <tr>
                                        <th>
                                            <!--<input type="checkbox" id="checkall" title="全选/全消"onclick="check()" />-->

                                            <!--<input type="button" value="全选/取消"onclick="check()" />-->
                                            <input type="button" value="反选" onclick="inverse()">
                                        <th>菜单名称</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr v-for="c in tableData">
                                        <td>
                                            <input  class="item"  :id="c.id" v-model="checkitemIds" type="checkbox" :value="c.id" >
                                        </td>
                                        <td><label :for="c.id">{{c.name}}</label></td>
                                    </tr>
                                    </tbody>
                                </table>

                            </el-tab-pane>

                            <el-tab-pane label="分配权限" name="second">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="名称">
                                                <el-input v-model="formData.name" readonly="readonly"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="关键字">
                                                <el-input v-model="formData.keyword" readonly="readonly"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                                <table class="datatable">
                                    <thead>
                                    <tr>
                                        <th>
                                            <!--<input type="checkbox" id="checkalll" title="全选/全消"onclick="checkk()" />-->
                                            <!--<input type="button" value="全选/取消"onclick="check()" />-->
                                            <input type="button" value="反选" onclick="inversee()"></th>
                                        <th>权限名称</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr v-for="c in findpermission">
                                        <td>
                                            <input :id="c.id" v-model="rolepermission" type="checkbox" :value="c.id" class="itemm">
                                        </td>
                                        <td><label :for="c.id">{{c.name}}</label></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="AccessTheMenu = false">取消</el-button>
                        <el-button type="primary" @click="oleleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    // function check(){
    //
    //     //获取checkall的checked属性值
    //     var v = document.getElementById("checkall").checked;
    //     //获取所有的item
    //     var items = document.getElementsByClassName("item");
    //     for (var i = 0; i < items.length; i++) {
    //         items[i].checked = v;
    //     }
    // }
    // function checkk(){
    //     //获取checkall的checked属性值
    //     var v = document.getElementById("checkalll").checked;
    //     //获取所有的item
    //     var items = document.getElementsByClassName("itemm");
    //     for (var i = 0; i < items.length; i++) {
    //         items[i].checked = v;
    //     }
    //
    // }
    /**
     * 反选：
     *  思路1：获取所有的item，循环遍历每一个，获取选中状态取反
     *  思路2：获取所有的item，循环遍历每一个，模拟一次点击操作
     */
    function inverse(){
        var items = document.getElementsByClassName("item");
        for (var i = 0; i < items.length; i++) {
            //items[i].checked =   (!items[i].checked);
            items[i].click();
        }
    }
    function inversee(){
        var items = document.getElementsByClassName("itemm");
        for (var i = 0; i < items.length; i++) {
            //items[i].checked =   (!items[i].checked);
            items[i].click();
        }
    }
</script>
<script>

    var vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData: [],//新增和编辑表单中对应的检查项列表数据
            findpermission: [],
            rolepermission: [],
            checkitemIds: [],
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false,//控制编辑窗口显示/隐藏
            AccessTheMenu: false,//控制权限窗口显示/隐藏
            rules: {
                keyword: [
                    {required: true, message: '角色关键字为必填项', trigger: 'blur'}
                ],
                name: [
                    {required: true, message: '角色名称为必填项', trigger: 'blur'}
                ]
            }

        },
        created() {
            this.findPage();

        },
        methods: {
            //菜单权限表
            oleleEdit() {
                axios.post("/roleInfo/menupermissionedit.do?menu=" + this.checkitemIds+"&permission="+this.rolepermission, this.formData).then(res => {
                    this.resetForm();
                    //弹出窗口
                    this.AccessTheMenu = false;
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? 'success' : 'error'
                    });
                    this.findPage();//分页查询
                })
            },
            //编辑
            handleEdit() {
                this.$refs["dataSetForm"].validate((valid) => {
                    if (valid) {
                        axios.post("/roleInfo/edit.do", this.formData).then(res => {
                            this.resetForm();
                            //弹出窗口
                            this.dialogFormVisible4Edit = false;
                            this.$message({
                                message: res.data.message,
                                type: res.data.flag ? 'success' : 'error'
                            });
                            this.findPage();//分页查询
                        })
                    }
                });
            },
            //添加
            handleAdd() {

                this.$refs["dataAddForm"].validate((valid) => {
                    if (valid) {
                        //发送请求将表单数据提交后台
                        //添加角色
                        axios.post("/roleInfo/add.do", this.formData).then(res => {
                            this.resetForm();
                            //弹出窗口
                            this.dialogFormVisible = false;
                            this.$message({
                                message: res.data.message,
                                type: res.data.flag ? 'success' : 'error'
                            });
                            this.findPage();//分页查询
                        })
                    }
                });
            },
            //分页查询
            findPage() {
                var parms = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };

                axios.post("/roleInfo/findPage.do", parms).then(res => {
                    //后台返回的是PageResult total rows
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                })
            },
            // 重置表单
            resetForm() {
                this.formData = {};
                this.checkitemIds = [];
                this.activeName = 'first';
                this.findpermission = [],
                    this.rolepermission = []
            },
            // 弹出添加窗口
            handleCreate() {
                //清空表单数据 以及 默认选中第一个选项卡
                this.resetForm();
                this.dialogFormVisible = true;
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                //弹出编辑窗口
                this.dialogFormVisible4Edit = true;
                // 并回显角色
                axios.get("/roleInfo/findById.do?roleId=" + row.id).then(res => {
                    this.formData = res.data.data;//回显角色数据
                    this.$message({
                        message: res.data.message,
                        type: 'success'
                    });
                })
            },
            //弹出编辑权限窗口
            roleleEdit(row) {
                this.AccessTheMenu = true;
                // 并回显角色
                axios.get("/roleInfo/findById.do?roleId=" + row.id).then(res => {
                    this.formData = res.data.data;//回显角色数据
                    this.$message({
                        message: res.data.message,
                        type: 'success'
                    });
                    //查询所有菜单
                    axios.get("/roleInfo/findAllMenu.do").then(res => {
                        this.tableData = res.data.data;//回显所有权限数据
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                        // 关联的权限   根据角色id 查询菜单id
                        axios.get("/roleInfo/findRoleMenuIdsByRoleId.do?roleId=" + row.id).then(res => {
                            //后台返回的List<Integer> [28,29,30]
                            this.checkitemIds = res.data;//回显的菜单勾选
                        })
                    })
                });
                axios.get("/roleInfo/queryAllPermissions.do").then(res => {
                    this.findpermission = res.data.data;//回显所有权限数据
                    this.$message({
                        message: res.data.message,
                        type: 'success'
                    });
                    // 根据角色id 查询权限id
                    axios.get("/roleInfo/findRolepermissionIdsByRoleId.do?roleId=" + row.id).then(res => {
                        //后台返回的List<Integer> [28,29,30]
                        this.rolepermission = res.data;//回显的检查项勾选
                    })
                })
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                this.$confirm('此操作将永久删除检查组记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送删除请求
                    axios.get("/roleInfo/deleteById.do?roleId=" + row.id).then(res => {
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? 'success' : 'error'
                        });
                        this.findPage();//分页查询
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    })
</script>
</html>
